package db;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author 史祥连
 * @Date 2024/1/29 11:35
 * @Version 1.0
 */
public class ConnectionHelper {
    public static Connection getConnection(final String url,final String username,final String password) throws SQLException {
        DataSource dataSource = createDataSource(url, username, password);
        return dataSource.getConnection();

    }

    private static DataSource createDataSource(final String url,final String username,final String password) {
        DruidDataSource result = new DruidDataSource();
        result.setDriverClassName(com.mysql.cj.jdbc.Driver.class.getName());
        result.setUrl(url);
        result.setUsername(username);
        result.setPassword(password);
        return result;
    }


    public static Connection getTestConnection() {
        Connection connection = null;
        try {
            connection = getConnection("jdbc:mysql://10.12.102.19:3306/ebuilder_form?characterEncoding=utf8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&allowMultiQueries=true", "database", "GGyasDd6sdTR7e");

        } catch (Exception e) {
            System.out.println("数据源异常");
        }
        return connection;

    }

    public static void main(String[] args) throws Exception{
        Connection connection = getConnection("jdbc:mysql://10.12.102.19:3306/ebuilder_form?characterEncoding=utf8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&allowMultiQueries=true", "database", "GGyasDd6sdTR7e");
        PreparedStatement preparedStatement = connection.prepareStatement("select * from uf_shxl_maven");
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            System.out.println(resultSet.getString("scope"));
        }
    }

}
